package com.android.marble;

/**
 * Plate.java
 * Define the Plate.
 * 
 * @author Daniel Deng
 * @create 6-29-2012
 * @update 6-30-2012
 */

import com.badlogic.gdx.math.Vector2;
import com.stickycoding.rokon.Sprite;
import com.stickycoding.rokon.Texture;


public class Plate extends Sprite {
	private float   mRadius;
	private Vector2 mOldPosition;
	private float   mWorldWidth;
	private float   mWorldHeight;

	public Plate(float x, float y,float radius,float worldWidth, float worldHeight) {
		super(x, y,radius*2,radius*2);
		mRadius = radius;
		mWorldWidth = worldWidth;
		mWorldHeight = worldHeight;
		mOldPosition = getCenterPosition();
	}
	/**
	 * create a Plate.
	 *
	 * @param texture define the texture of the PLATE
	 */
	public void creat(Texture texture){
		//CircleShape circle = new CircleShape();
		//circle.setRadius(mRadius);
		//circle.dispose();
		
		setTexture(texture);		
	}
	/**
	 * Set the  position of Plate
	 *
	 * @param x
	 * @param y
	 */
	public void setPosition(float x, float y) {
		super.x = x - mRadius;
		super.y = y - mRadius;
	}
	/**
	 * return the  position of Plate
	 * @return center position
	 */
	public final Vector2 getCenterPosition() {
		Vector2 pos = new Vector2(super.x+mRadius,super.y+mRadius);
		return pos;
	}
	/**
	 * return the  radius of plate
	 * @return radius of plate
	 */
	public final float getRadius() {
		return mRadius;
	}
	/**
	 * return current Speed of plate
	 * @param dettaT
	 * @return speed
	 */
	public final float getSpeed(float dettaT) {
		float currentSpeed = 0f;
		float dettaS = (float) Math.sqrt(Math.pow(mOldPosition.x-getCenterPosition().x,2)+Math.pow(mOldPosition.y-getCenterPosition().y,2));
		currentSpeed = dettaS / dettaT;
		
		return currentSpeed;
	}
	/**
	 * update status of plate
	 */
	public final void update() {
		verifyPosition();
	}
	/**
	 * verify the position of plate. To prevent plate move to illegal area
	 */
	private void verifyPosition() {
		float oX = getCenterPosition().x;
		float oY = getCenterPosition().y;
		if (oX - mRadius < 0)oX = mRadius;
		if (oX + mRadius > mWorldWidth)oX=mWorldWidth - mRadius;
		if (oY - mRadius < 0)oY = mRadius;
		if (oY + mRadius > mWorldHeight)oY = mWorldHeight - mRadius;
		setPosition(oX,oY);
	}
}
